package com.xiaoxing.zhinenghuacangku_nanjinglu.mvp.ui.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dds.soap.SoapListener;
import com.dds.soap.SoapParams;
import com.dds.soap.SoapUtil;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.base.BaseConstants;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.LogUtils;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xiaoxing.zhinenghuacangku_nanjinglu.R;
import com.xiaoxing.zhinenghuacangku_nanjinglu.R2;
import com.xiaoxing.zhinenghuacangku_nanjinglu.di.component.DaggerNanJingLu_DianBiaoChuKuComponent;
import com.xiaoxing.zhinenghuacangku_nanjinglu.di.module.NanJingLu_DianBiaoChuKuModule;
import com.xiaoxing.zhinenghuacangku_nanjinglu.mvp.contract.NanJingLu_DianBiaoChuKuContract;
import com.xiaoxing.zhinenghuacangku_nanjinglu.mvp.presenter.NanJingLu_DianBiaoChuKuPresenter;
import com.xiaoxing.zhinenghuacangku_nanjinglu.mvp.ui.adapter.NanJingLu_DianBiaoChuKuAdapter;
import com.xiaoxing.zhinenghuacangku_nanjinglu.mvp.ui.adapter.NanJingLu_MeterSearchConditionAdapter;
import com.xiaoxing.zhinenghuacangku_nanjinglu.mvp.ui.entity.NanJingLu_DianBiaoChuKu;
import com.xiaoxing.zhinenghuacangku_nanjinglu.mvp.ui.entity.NanJingLu_GetMeterSearchCondition;
import com.xiaoxing.zhinenghuacangku_nanjinglu.mvp.ui.entity.NanJingLu_MeterOutStorageListInfo;
import com.xiaoxing.zhinenghuacangku_nanjinglu.mvp.ui.entity.XinyemianhuanchongweiBean;

import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import com.jess.arms.utils.CProgressDialogUtils;
import me.jessyan.armscomponent.commonres.utils.ToolbarUtils;
import me.jessyan.armscomponent.commonres.view.NiceSpinner;
import me.jessyan.armscomponent.commonres.view.RecycleViewDivider;
import me.jessyan.armscomponent.commonsdk.core.RouterHub;

import static com.jess.arms.utils.Preconditions.checkNotNull;


@Route(path = RouterHub.ZHI_NENG_HUA_CANG_KU_NANJINGLU_ACTIVITY_NAN_JING_LU__DIAN_BIAO_CHU_KU)
public class NanJingLu_DianBiaoChuKuActivity extends BaseActivity<NanJingLu_DianBiaoChuKuPresenter> implements NanJingLu_DianBiaoChuKuContract.View, OnRefreshListener, NanJingLu_MeterSearchConditionAdapter.IMeterSearchConditionAdapter, NanJingLu_DianBiaoChuKuAdapter.INanJingLu_DianBiaoChuKuAdapter {


    private static boolean mIsNeedDemo = true;
    @BindView(R2.id.empty_text)
    TextView empty_text;
    @BindView(R2.id.empty_image)
    ImageView empty_image;
    @BindView(R2.id.empty)
    View mEmptyLayout;
    @BindView(R2.id.recyclerView_data)
    RecyclerView mRecyclerViewData;
    @BindView(R2.id.recyclerView_condition)
    RecyclerView mRecyclerViewCondition;
    @BindView(R2.id.refreshLayout)
    RefreshLayout mRefreshLayout;
    @BindView(R2.id.nsv)
    NestedScrollView nsv;
    @BindView(R2.id.ns_1)
    NiceSpinner ns1;
    @BindView(R2.id.ns_2)
    NiceSpinner ns2;
    @BindView(R2.id.et_remark)
    EditText etRemark;
    @BindView(R2.id.et_num)
    EditText etNum;
    @BindView(R2.id.btn_agv_chu_ku)
    Button btnAgvChuKu;
    @BindView(R2.id.btn_generate_tags)
    Button btnGenerateTags;
    private NanJingLu_DianBiaoChuKuAdapter mAdapter;
    private List<NanJingLu_MeterOutStorageListInfo> mDataBeanList = new ArrayList<>();
    private ArrayList<String> mNs1List1 = new ArrayList<>();
    private ArrayList<String> mNs2List2 = new ArrayList<>();
    private NanJingLu_MeterSearchConditionAdapter mMeterSearchConditionAdapter;
    private List<String> mMeterSearchConditionList = new ArrayList<>();
    private HashMap<String, ArrayList<String>> valuesHashMap = new HashMap<>();
    private HashMap<String, String> valuenamesHashMap = new HashMap<>();
    private String mLingLiaoLeiXing = "";
    private String mSearchcondition = "";
    private String mSelectedDisplayname = "";
    private String mSelectedValuename = "";
    private String mSelectedValue = "";

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerNanJingLu_DianBiaoChuKuComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .nanJingLu_DianBiaoChuKuModule(new NanJingLu_DianBiaoChuKuModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_nan_jing_lu__dian_biao_chu_ku; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        ToolbarUtils.initToolbarTitleBack(this, getString(R.string.activity_nan_jing_lu__dian_biao_chu_ku));

        initRefreshLayout();
        initRecyclerViewData();
        initEmpty();
        //getNanJingLu_DianBiaoChuKuList();
        initRecyclerViewCondition();
        try {
            GetMeterSearchCondition();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initRefreshLayout() {
        mRefreshLayout.setRefreshHeader(new
                ClassicsHeader(this).setSpinnerStyle(SpinnerStyle.FixedBehind).setPrimaryColorId(R.color.public_colorPrimary).setAccentColorId(android.R.color.white));
        mRefreshLayout.setOnRefreshListener(this);
        mRefreshLayout.setEnableRefresh(false);
        mRefreshLayout.autoRefresh();
        mRefreshLayout.setEnableLoadMore(false);
    }

    private void initRecyclerViewData() {
        mRecyclerViewData.setLayoutManager(new LinearLayoutManager(NanJingLu_DianBiaoChuKuActivity.this));
        mRecyclerViewData.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.VERTICAL, 10, Color.parseColor("#EEEEEE")));

        mRecyclerViewData.setItemAnimator(new DefaultItemAnimator());
        mRecyclerViewData.setAdapter(mAdapter = new NanJingLu_DianBiaoChuKuAdapter(NanJingLu_DianBiaoChuKuActivity.this, mDataBeanList, this));

        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
            }
        });
    }

    private void initEmpty() {
        empty_image.setImageResource(R.drawable.ic_empty);
        empty_text.setTextColor(Color.parseColor("#CFCFCF"));
        empty_text.setText("暂无数据");
    }

    private void initRecyclerViewCondition() {
        mRecyclerViewCondition.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.HORIZONTAL, 0, getResources().getColor(R.color.public_client_line_color)));
        mMeterSearchConditionAdapter = new NanJingLu_MeterSearchConditionAdapter(getContext(), mMeterSearchConditionList, this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerViewCondition.setLayoutManager(linearLayoutManager);
        mRecyclerViewCondition.setAdapter(mMeterSearchConditionAdapter);
    }

    /**
     * @param
     * @return
     * @method
     * @description 获取查询电表列表的查询条件
     * @date: 2020/6/15 0015 15:43
     * @author: 小星
     */
    private void GetMeterSearchCondition() {

        CProgressDialogUtils.showProgressDialog(this);
        String methodName = "GetMeterSearchCondition";
        SoapParams params = new SoapParams();

        /**
         * 请求
         */
        SoapUtil.getInstance(this).callEntry(getUrl(), BaseConstants.NAME_SPACE, methodName, params, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {
                CProgressDialogUtils.cancelProgressDialog();


                String result = object.getProperty(methodName + "Result").toString();
                try {
                    if (result != null && !result.equals("[]") && !result.equals("anyType{}") && !result.equals("Error") && !result.equals("-100")) {
                        List<NanJingLu_GetMeterSearchCondition> getMeterSearchConditionList = new Gson().fromJson(result, new TypeToken<List<NanJingLu_GetMeterSearchCondition>>() {
                        }.getType());


                        mNs1List1.add("请选择");

                        for (int i = 0; i < getMeterSearchConditionList.size(); i++) {
                            mNs1List1.add(getMeterSearchConditionList.get(i).getDisplayname());
                            valuenamesHashMap.put(getMeterSearchConditionList.get(i).getDisplayname(), getMeterSearchConditionList.get(i).getValuename());
                            valuesHashMap.put(getMeterSearchConditionList.get(i).getDisplayname(), getMeterSearchConditionList.get(i).getValuelist());

                        }
                        //                    dataNames.add("接线方式@connmode");
                        //                    dataNames.add("电压@elecpress");
                        //                    dataNames.add("容量@volume");
                        //                    dataNames.add("显示方式@displaytype");
                        //                    dataNames.add("有功等级@workgrade");
                        //                    dataNames.add("无功等级@noworkgrade");
                        //                    dataNames.add("通讯方式@commtype");


                        ns2.attachDataSource(mNs2List2);
//                        ns2.addOnItemClickListener(new AdapterView.OnItemClickListener() {
//                            @Override
//                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//
//                                LogUtils.debugEInfo("==============spinner_2_onItemSelected===============");
//
//                                if (position != 0) {
//
//                                    mSelectedValue = String.valueOf(ns2.getFullWidthAdapter().getItem(position));
//                                    LogUtils.debugEInfo("mSelectedValue=" + mSelectedValue);
//
//                                    if (!mSelectedValue.equals("请选择")) {
//                                        if (mMeterSearchConditionList.size() > 0) {
//                                            for (int i = 0; i < mMeterSearchConditionList.size(); i++) {//没一个条件只能选择一个
//
//                                                if (mMeterSearchConditionList.get(i).contains(mSelectedDisplayname)) {
//                                                    mMeterSearchConditionList.remove(i);
//                                                }
//                                            }
//                                        }
//
//                                        mMeterSearchConditionList.add(mSelectedDisplayname + ":" + mSelectedValue);
//                                        mMeterSearchConditionAdapter.notifyDataSetChanged();
//
//                                        mDataBeanList.clear();
//                                        mAdapter.notifyDataSetChanged();
//                                        mEmptyLayout.setVisibility(View.VISIBLE);
//                                        mRecyclerViewData.setVisibility(View.GONE);
//
////                                        getGetOutStorageMeterListInfo();
//
////                                        Handler handler = new Handler();
////                                        handler.post(new Runnable() {
////                                            @Override
////                                            public void run() {
////                                                //设置ScrollView滚动到顶部
////                                                nsv.fullScroll(ScrollView.FOCUS_DOWN);
////
////                                                //设置ScrollView滚动到顶部
////                                                //scrollView.fullScroll(ScrollView.FOCUS_DOWN);
////                                            }
////                                        });
//                                    }
//                                }
//                            }
//                        });


                        ns1.attachDataSource(mNs1List1);
                        ns1.addOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


                                LogUtils.debugEInfo("==============spinner_1_onItemSelected===============");
                                if (position != 0) {

                                    mSelectedDisplayname = (String) ns1.getFullWidthAdapter().getItem(position);
                                    mSelectedValuename = valuenamesHashMap.get(mSelectedDisplayname);

                                    LogUtils.debugEInfo("mSelectedDisplayname=" + mSelectedDisplayname);
                                    LogUtils.debugEInfo("mSelectedValuename=" + mSelectedValuename);
                                    LogUtils.debugEInfo("valuesHashMap.get(mSelectedDisplayname)=" + valuesHashMap.get(mSelectedDisplayname));

                                    mNs2List2.clear();
                                    mNs2List2.add("请选择");
                                    mNs2List2.addAll(valuesHashMap.get(mSelectedDisplayname));
                                    ns2.attachDataSource(mNs2List2);
                                    //                                mSelectedValue = String.valueOf(spinnerAdapter2.getItem(0));
                                    //                                LogUtils.debugEInfo("mSelectedValue=" + mSelectedValue);
                                    //设置spinner内的填充文字居中
                                    //((TextView)view).setGravity(Gravity.CENTER);
                                    ns2.addOnItemClickListener(null);
                                    ns2.addOnItemClickListener(new AdapterView.OnItemClickListener() {
                                        @Override
                                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                                            LogUtils.debugEInfo("==============spinner_2_onItemSelected===============");

                                            if (position != 0) {

                                                mSelectedValue = String.valueOf(ns2.getFullWidthAdapter().getItem(position));
                                                LogUtils.debugEInfo("mSelectedValue=" + mSelectedValue);

                                                if (!mSelectedValue.equals("请选择")) {
                                                    if (mMeterSearchConditionList.size() > 0) {
                                                        for (int i = 0; i < mMeterSearchConditionList.size(); i++) {//没一个条件只能选择一个

                                                            if (mMeterSearchConditionList.get(i).contains(mSelectedDisplayname)) {
                                                                mMeterSearchConditionList.remove(i);
                                                            }
                                                        }
                                                    }

                                                    mMeterSearchConditionList.add(mSelectedDisplayname + ":" + mSelectedValue);
                                                    mMeterSearchConditionAdapter.notifyDataSetChanged();

                                                    mDataBeanList.clear();
                                                    mAdapter.notifyDataSetChanged();
                                                    mEmptyLayout.setVisibility(View.VISIBLE);
                                                    mRecyclerViewData.setVisibility(View.GONE);

//                                        getGetOutStorageMeterListInfo();

//                                        Handler handler = new Handler();
//                                        handler.post(new Runnable() {
//                                            @Override
//                                            public void run() {
//                                                //设置ScrollView滚动到顶部
//                                                nsv.fullScroll(ScrollView.FOCUS_DOWN);
//
//                                                //设置ScrollView滚动到顶部
//                                                //scrollView.fullScroll(ScrollView.FOCUS_DOWN);
//                                            }
//                                        });
                                                }
                                            }
                                        }
                                    });
                                }
                            }
                        });

                    } else {
                    }
                } catch (JsonSyntaxException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
                CProgressDialogUtils.cancelProgressDialog();

            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
                CProgressDialogUtils.cancelProgressDialog();


            }
        }, getAapiUserName(), getApiPwd());

    }

    private List<NanJingLu_DianBiaoChuKu.DataBean> loadModels() {
        List<NanJingLu_DianBiaoChuKu.DataBean> lists = new ArrayList<>();

        for (int i = 0; i < 5; i++) {

            NanJingLu_DianBiaoChuKu.DataBean dataBean = new NanJingLu_DianBiaoChuKu.DataBean();
            lists.add(dataBean);
        }

        return lists;
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {

        //getNanJingLu_DianBiaoChuKuList();
        mRefreshLayout.finishRefresh();
        mEmptyLayout.setVisibility(View.GONE);

    }

    @Override
    public void getNanJingLu_DianBiaoChuKuDataSuccess(NanJingLu_DianBiaoChuKu entityList) {

//        if (entityList != null && entityList.getData() != null && entityList.getData().size() > 0) {
//            mDataBeanList.clear();
//            mDataBeanList.addAll(entityList.getData());
//            mAdapter.notifyDataSetChanged();
//            mEmptyLayout.setVisibility(View.GONE);
//            mRecyclerViewData.setVisibility(View.VISIBLE);
//        } else {
//            mEmptyLayout.setVisibility(View.VISIBLE);
//            mRecyclerViewData.setVisibility(View.GONE);
//        }

    }

    private void getNanJingLu_DianBiaoChuKuList() {
        HashMap<String, String> map = new HashMap<>();

        mPresenter.getNanJingLu_DianBiaoChuKuList(map);
    }

    @Override
    public void delMeterSearchCondition(String item) {
        mMeterSearchConditionList.remove(item);
        mMeterSearchConditionAdapter.notifyDataSetChanged();

        mDataBeanList.clear();
        mAdapter.notifyDataSetChanged();
        mEmptyLayout.setVisibility(View.VISIBLE);
        mRecyclerViewData.setVisibility(View.GONE);
//        Handler handler = new Handler();
//        handler.post(new Runnable() {
//            @Override
//            public void run() {
//                //设置ScrollView滚动到顶部
//                nsv.fullScroll(ScrollView.FOCUS_DOWN);
//
//                //设置ScrollView滚动到顶部
//                //scrollView.fullScroll(ScrollView.FOCUS_DOWN);
//            }
//        });

        getGetOutStorageMeterListInfo();
    }

    public void getGetOutStorageMeterListInfo() {
        if (mMeterSearchConditionList.size() > 0) {
            String jsonData = "[";
            for (int i = 0; i < mMeterSearchConditionList.size(); i++) {
                String data = mMeterSearchConditionList.get(i);
                if (data.contains(":")) {
                    jsonData += "{\"Key\":\"" + valuenamesHashMap.get(data.split(":")[0]) + "\",\"Value\":\"" + data.split(":")[1] + "\"},";
                }

            }
            jsonData = ArmsUtils.cutLastString(jsonData);
            jsonData += "]";
            LogUtils.debugEInfo("jsonData===" + jsonData);
            GetOutStorageMeterListInfo(jsonData);
        }
    }

    /**
     * @param
     * @return
     * @method
     * @description 根据条件查询电表数据列表
     * @date: 2020/6/15 0015 15:43
     * @author: 小星
     */
    public void GetOutStorageMeterListInfo(String searchcondition) {
        showLoading();
        String methodName = "GetOutStorageMeterListInfo";
        SoapParams params = new SoapParams();
        params.put("searchcondition", searchcondition);

        SoapUtil.getInstance(this).callEntry(getUrl(), BaseConstants.NAME_SPACE, methodName, params, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {
                hideLoading();

                try {
                    String result = object.getProperty(methodName + "Result").toString();
                    List<NanJingLu_MeterOutStorageListInfo> nanJingLu_meterOutStorageListInfos = new Gson().fromJson(result, new TypeToken<List<NanJingLu_MeterOutStorageListInfo>>() {
                    }.getType());
                    LogUtils.debugEInfo("result==" + result);

                    if (nanJingLu_meterOutStorageListInfos != null && nanJingLu_meterOutStorageListInfos.size() > 0) {
                        mDataBeanList.clear();
                        mDataBeanList.addAll(nanJingLu_meterOutStorageListInfos);
                        mAdapter.notifyDataSetChanged();
                        mEmptyLayout.setVisibility(View.GONE);
                        mRecyclerViewData.setVisibility(View.VISIBLE);
                    } else {
                        mEmptyLayout.setVisibility(View.VISIBLE);
                        mRecyclerViewData.setVisibility(View.GONE);
                    }
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
                hideLoading();
                showMessage(content);
            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
                hideLoading();
                showMessage(fault.getMessage());

            }
        }, getAapiUserName(), getApiPwd());
    }

    @Override
    public void showLoading() {
        CProgressDialogUtils.showProgressDialog(this);
    }

    @Override
    public void hideLoading() {
        CProgressDialogUtils.cancelProgressDialog(this);
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.makeText(this, message);

    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public void chuKuAGV(NanJingLu_MeterOutStorageListInfo item) { //出库先查询，选择出库缓冲位置，选择完出库位置之后执行出库操作
        getChuKuAGVHuanChongWei("0", item);
    }

    @Override
    public void chuKuLiKu(NanJingLu_MeterOutStorageListInfo item) {
        getChuKuLiKuHuanChongWei("0", item);
    }

    /**
     * @param
     * @return
     * @method
     * @description 查询立库出库位置列表
     * @date: 2020/6/15 0015 16:08
     * @author: 小星
     */
    public void getChuKuLiKuHuanChongWei(String out_storageList_id, NanJingLu_MeterOutStorageListInfo item) {
        showLoading();
        String methodNames = "GetLiKuEntryBuffer";
        SoapParams params = new SoapParams();
        params.put("out_storagelistId", out_storageList_id);

        SoapUtil.getInstance(getContext()).callEntry(getUrl(), BaseConstants.NAME_SPACE, methodNames, params, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {
                hideLoading();

                String result = object.getProperty(methodNames + "Result").toString();
                if (result != null && !result.equals("[]") && !result.equals("anyType{}") && !result.equals("Error") && !result.equals("-100")) {
                    try {

                        List<XinyemianhuanchongweiBean> xinyemianhuanchongweiBeanList = ArmsUtils.parseString2List(result, XinyemianhuanchongweiBean.class);
                        showXuanZeChuKuLiKuCangWeiDialog(xinyemianhuanchongweiBeanList, out_storageList_id, item);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                } else {
                    showMessage("没有出库位可选");
                }
            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
                hideLoading();

            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
                hideLoading();

            }
        }, getAapiUserName(), getApiPwd());
    }

    /**
     * @param
     * @return
     * @method
     * @description 选择立库出库仓位弹出框
     * @date: 2020/6/15 0015 16:07
     * @author: 小星
     */
    public void showXuanZeChuKuLiKuCangWeiDialog(List<XinyemianhuanchongweiBean> xinyemianhuanchongweiBeans, String out_storageList_id, NanJingLu_MeterOutStorageListInfo item) {

        ArrayList<String> list = new ArrayList<String>();
        HashMap<String, String> dataIds = new HashMap<>();

        for (int i = 0; i < xinyemianhuanchongweiBeans.size(); i++) {
            list.add(xinyemianhuanchongweiBeans.get(i).getBuffer_name() + "(" + (xinyemianhuanchongweiBeans.get(i).getBuffer_status().equals("0") ? "空闲" : "有任务") + ")");
            dataIds.put(xinyemianhuanchongweiBeans.get(i).getBuffer_name() + "(" + (xinyemianhuanchongweiBeans.get(i).getBuffer_status().equals("0") ? "空闲" : "有任务") + ")", xinyemianhuanchongweiBeans.get(i).getId());
        }

        String[] items = new String[list.size()];
        list.toArray(items);

        QMUIDialog.CheckableDialogBuilder builder = new QMUIDialog.CheckableDialogBuilder(getContext())
                .setCheckedIndex(0)
                .setTitle("选择出库仓位")
                .addItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });

        builder.addAction("取消", new QMUIDialogAction.ActionListener() {
            @Override
            public void onClick(QMUIDialog dialog, int index) {
                dialog.dismiss();
            }
        });
        builder.addAction("提交", new QMUIDialogAction.ActionListener() {
            @Override
            public void onClick(QMUIDialog dialog, int index) {

                String selectedId = dataIds.get(items[builder.getCheckedIndex()]);
                LogUtils.debugEInfo("selectedId=" + selectedId);
                CallAutoMachineGetMeterDirect(item.getMid(), selectedId, item.getSpid(), item.getSpacename());

                dialog.dismiss();

            }
        });
        builder.create(com.qmuiteam.qmui.R.style.QMUI_Dialog).show();
    }

    /**
     * @param
     * @return
     * @method
     * @description 立库出库
     * @date: 2020/6/15 0015 16:04
     * @author: 小星
     */
    public void CallAutoMachineGetMeterDirect(String mid, String outbufferid, String spid, String spacename) {
        showLoading();
        String methodName = "CallAutoMachineGetMeterDirect";
        SoapParams params = new SoapParams();
        params.put("mid", mid);
        params.put("outbufferid", outbufferid);
        params.put("spid", spid);
        params.put("spname", spacename);

        SoapUtil.getInstance(this).callEntry(getUrl(), BaseConstants.NAME_SPACE, methodName, params, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {
                hideLoading();

                try {
                    String result = object.getProperty(methodName + "Result").toString();
                    if (result.equals("true")) {
                        showMessage("正在出库...");
                    } else {
                        showMessage("出库失败");
                    }

                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
                hideLoading();
                showMessage(content);
            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
                hideLoading();
                showMessage(fault.getMessage());

            }
        }, getAapiUserName(), getApiPwd());
    }

    /**
     * @param out_storageList_id 默认为0 仅限青岛南京路仓库-电表
     * @return
     * @method
     * @description 获取agv出库缓冲位置
     * @date: 2020/6/15 0015 15:57
     * @author: 小星
     */
    public void getChuKuAGVHuanChongWei(String out_storageList_id, NanJingLu_MeterOutStorageListInfo item) {
        showLoading();
        String methodNames = "GetBatchFreeOutStoreBuffer";

        SoapParams params = new SoapParams();
        params.put("out_storagelistId", out_storageList_id);

        String finalMethodNames = methodNames;
        SoapUtil.getInstance(getContext()).callEntry(getUrl(), BaseConstants.NAME_SPACE, methodNames, params, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {
                hideLoading();

                String result = object.getProperty(finalMethodNames + "Result").toString();
                if (result != null && !result.equals("[]") && !result.equals("anyType{}") && !result.equals("Error") && !result.equals("-100")) {
                    try {

                        List<XinyemianhuanchongweiBean> xinyemianhuanchongweiBeanList = ArmsUtils.parseString2List(result, XinyemianhuanchongweiBean.class);
                        showXuanZeChuKuAGVCangWeiDialog(xinyemianhuanchongweiBeanList, out_storageList_id, item);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                } else {
                    showMessage("没有出库位可选");
                }
            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
                hideLoading();

            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
                hideLoading();

            }
        }, getAapiUserName(), getApiPwd());
    }

    /**
     * @param
     * @return
     * @method
     * @description 选择AGV出库仓位弹出框
     * @date: 2020/6/15 0015 16:07
     * @author: 小星
     */
    public void showXuanZeChuKuAGVCangWeiDialog(List<XinyemianhuanchongweiBean> xinyemianhuanchongweiBeans, String out_storageList_id, NanJingLu_MeterOutStorageListInfo item) {

        ArrayList<String> list = new ArrayList<String>();
        HashMap<String, String> dataIds = new HashMap<>();

        for (int i = 0; i < xinyemianhuanchongweiBeans.size(); i++) {
            list.add(xinyemianhuanchongweiBeans.get(i).getBuffer_name());
            dataIds.put(xinyemianhuanchongweiBeans.get(i).getBuffer_name(), xinyemianhuanchongweiBeans.get(i).getId());
        }

        String[] items = new String[list.size()];
        list.toArray(items);

        QMUIDialog.CheckableDialogBuilder builder = new QMUIDialog.CheckableDialogBuilder(getContext())
                .setCheckedIndex(0)
                .setTitle("选择出库仓位")
                .addItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });

        builder.addAction("取消", new QMUIDialogAction.ActionListener() {
            @Override
            public void onClick(QMUIDialog dialog, int index) {
                dialog.dismiss();
            }
        });
        builder.addAction("提交", new QMUIDialogAction.ActionListener() {
            @Override
            public void onClick(QMUIDialog dialog, int index) {

                try {
                    String selectedId = dataIds.get(items[builder.getCheckedIndex()]);
                    LogUtils.debugEInfo("selectedId=" + selectedId);
                    CallAutoMachineGetMeterAgv(item.getMid(), selectedId, item.getSpid(), item.getSpacename());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                dialog.dismiss();

            }
        });
        builder.create(com.qmuiteam.qmui.R.style.QMUI_Dialog).show();
    }

    /**
     * @param
     * @return
     * @method
     * @description AGV出库
     * @date: 2020/6/15 0015 15:45
     * @author: 小星
     */
    public void CallAutoMachineGetMeterAgv(String mid, String outbufferid, String spid, String spacename) {
        showLoading();
        String methodName = "CallAutoMachineGetMeterAgv";
        SoapParams params = new SoapParams();
        params.put("mid", mid);
        params.put("outbufferid", outbufferid);
        params.put("spid", spid);
        params.put("spname", spacename);

        SoapUtil.getInstance(this).callEntry(getUrl(), BaseConstants.NAME_SPACE, methodName, params, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {
                hideLoading();

                try {
                    String result = object.getProperty(methodName + "Result").toString();
                    if (result.equals("true")) {
                        showMessage("正在出库...");
                        getGetOutStorageMeterListInfo();
                    } else {
                        showMessage("出库失败");
                    }

                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
                hideLoading();
                showMessage(content);
            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
                hideLoading();
                showMessage(fault.getMessage());

            }
        }, getAapiUserName(), getApiPwd());
    }

    @OnClick({R2.id.btn_agv_chu_ku, R2.id.btn_generate_tags})
    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.btn_agv_chu_ku) {
            chuku(1);
        } else if (id == R.id.btn_generate_tags) {
            chuku(2);
        }
    }

    public void chuku(int agvOrLiKu) {
        checkNullValue();

        if (mMeterSearchConditionList.size() > 0) {
            String jsonData = "[";
            for (int i = 0; i < mMeterSearchConditionList.size(); i++) {
                String data = mMeterSearchConditionList.get(i);
                if (data.contains(":")) {
                    jsonData += "{\"Key\":\"" + valuenamesHashMap.get(data.split(":")[0]) + "\",\"Value\":\"" + data.split(":")[1] + "\"},";
                }

            }
            jsonData = ArmsUtils.cutLastString(jsonData);
            jsonData += "]";
            LogUtils.debugEInfo("jsonData===" + jsonData);
            OutStoreMeter(agvOrLiKu, jsonData);
        } else {
            showMessage("请选择查询条件");
        }
    }

    public void checkNullValue() {
        if (TextUtils.isEmpty(getetRemark())) {
            showMessage("请填写领料单位");
            return;
        }
        if (TextUtils.isEmpty(getetNum())) {
            showMessage("请填写数量");
            return;
        }
    }

    /**
     * @param agvOrLiKu 1是AGV 2是立库
     * @return
     * @method
     * @description 电表出库
     * @date: 2020/6/17 0017 11:37
     * @author: 小星
     */
    private void OutStoreMeter(int agvOrLiKu, String jsonData) {

        CProgressDialogUtils.showProgressDialog(this);
        String methodName = "OutStoreMeter";
        SoapParams params = new SoapParams();
        params.put("num", getetNum());
        params.put("remark", getetRemark());
        params.put("searchcondition", jsonData);
        params.put("agvOrLiKu", String.valueOf(agvOrLiKu));
        /**
         * 请求
         */
        SoapUtil.getInstance(this).callEntry(getUrl(), BaseConstants.NAME_SPACE, methodName, params, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {
                CProgressDialogUtils.cancelProgressDialog();


                String result = object.getProperty(methodName + "Result").toString();
                try {
                    showMessage(result);
                } catch (JsonSyntaxException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
                CProgressDialogUtils.cancelProgressDialog();

            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
                CProgressDialogUtils.cancelProgressDialog();


            }
        }, getAapiUserName(), getApiPwd());

    }

    private String getetRemark() {
        return etRemark.getText().toString().trim();
    }

    private String getetNum() {
        return etNum.getText().toString().trim();
    }

}
